package view;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Scanner;

import checking.InputValidation;
import controller.ControllerForDB;

import model.Project;

public class MenuHelperForProject {

	static InputValidation oValidation = new InputValidation();
	static ControllerForDB controller = new ControllerForDB();
	static int sResp = 0;
	
	static Project updatedProject = null;
	static MenuHelperForActivity oActivityHelper = new MenuHelperForActivity();

	@SuppressWarnings("null")
	public void addProjectFromMenu() throws ParseException {
		Project addedProject = null;

		System.out.println("Please enter the name of the project;\n");
		Scanner proName = new Scanner(System.in);
		String name = proName.next();
		System.out.println("\n");

		try {

			oValidation.validateString(name);
			ArrayList<Project> proListFromDB = new ArrayList<Project>();
			proListFromDB = controller.getAllProjects();
			oValidation.validateStringForProjectName(name, proListFromDB);

		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println("Please enter valid entries!");

		}

		System.out.println("Please enter the description of the project;\n");
		Scanner proDesc = new Scanner(System.in);
		String desc = proDesc.next();
		System.out.println("\n");

		try {

			oValidation.validateString(desc);

		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println("Please enter valid entries!");

		}

		System.out
				.println("Please enter the start date of the project as format 'dd-MM-yyyy';\n");
		Scanner proStartDate = new Scanner(System.in);
		String proDate = proStartDate.next();
		if (!(oValidation.date_control(proDate))) {
			System.out.println("Invalid date entry!!!");

		}

		System.out.println("Please enter PM id;\n");
		Scanner proManagerIde = new Scanner(System.in);
		int pMIde = proManagerIde.nextInt();
		System.out.println("\n");

		controller.addProjectToDB(name, desc, proDate, pMIde);

		System.out
				.println("If you want to continue, these are your options;\n");

		System.out.println("Options;\n");
		System.out.println("1- Add Activity\n");
		System.out.println("2- Exit");
		System.out.println("\n");

		Scanner response2 = new Scanner(System.in);
		String secondResp = response2.next();
		sResp = oValidation.checkInteger(secondResp);
		oValidation.validateResponse(sResp, 2);

		if (sResp == 1) {
			oActivityHelper.addActivityToProject(addedProject.getNumber());
		}

	}

	public void updateProjectFromMenu() throws ParseException {

		System.out.println("Please enter PM id;\n");
		Scanner proManagerId = new Scanner(System.in);
		String pMId = proManagerId.next();
		int proManId = oValidation.checkInteger(pMId);

		System.out.println("Your projects are:\n");
		controller.getAllProjectsForPM(proManId);

		System.out
				.println("Please enter the the project id that you want to update :");

		Scanner proNumber = new Scanner(System.in);
		String pNumber = proNumber.next();// update edilmek istenen proje//
											// numarasi
		int proNumb = oValidation.checkInteger(pNumber);

		// update project menu
		System.out.println("      Update Options:           \n");
		System.out.println("1- Update Project  Specifications\n");
		System.out.println("2- Add    Activity to Project\n");
		System.out.println("3- Remove Activity from Project\n ");
		System.out.println("4- Update Activity in Project\n");
		System.out.println("5- Exit");

		Scanner response4 = new Scanner(System.in);// update project response
													// from console
		String fifthResp = response4.next();
		int fifResp = oValidation.checkInteger(fifthResp);

		switch (fifResp)// update project menu
		{

		case 1:// update project specifications
			updateProjectSpec(proNumb);
			break;// Update Project StartDate ends

		case 2: // add activity to an exist project
			oActivityHelper.addActivityToProject(proNumb);
			// add activity menu ends in update project menu

		case 3:// Remove Activity from Project
			oActivityHelper.removeActivityFromProject(proNumb);
			break;

		case 4:// update activity in the project
			oActivityHelper.updateActivityFromProject(proNumb);
			break;

		case 5:// Exit from update project
			System.out.println("Good Bye!");
			break;

		default:
			break;

		}// update project ends
	}

	public void removeProjectFromMenu() {

		System.out.println("Please enter PM id;\n");
		Scanner proManagerId = new Scanner(System.in);
		String pMId = proManagerId.next();
		int proManId = oValidation.checkInteger(pMId);

		controller.getAllProjectsForPM(proManId);

		System.out
				.println("Please enter Project id that you want to remove;\n");
		Scanner proNumber = new Scanner(System.in);
		String pNumber = proNumber.next();
		int proNumb = oValidation.checkInteger(pNumber);

		controller.removeProjectFromDB(proNumb);

	}

	public static void updateProjectSpec(int proId) {
		System.out.println("Please enter new project name:\n");
		Scanner proName1 = new Scanner(System.in);
		String pName = proName1.next();
		updatedProject.setName(pName);

		System.out.println("new project name:" + updatedProject.getName());

		System.out.println("Please enter new project description:\n");
		Scanner proDesc1 = new Scanner(System.in);
		String pDesc = proDesc1.next();
		updatedProject.setDesc(pDesc);

		System.out.println("new project description: "
				+ updatedProject.getDesc());

		System.out.println("Please enter new project start date:\n");
		Scanner proStartDate1 = new Scanner(System.in);
		String pSD = proStartDate1.next();
		if (!(oValidation.date_control(pSD)))
			System.out.println("Invalid date entry!!!");

		updatedProject.setStartDate(pSD);

		System.out.println("New project start date:"
				+ updatedProject.getStartDate());

		controller.updateProjectFromDB(proId,pName,pDesc,pSD);
	}



	

	}


